package com.xcity.game.battle.pve;

import com.alibaba.fastjson.annotation.JSONField;
import com.xcity.db.entity.Player;
import com.xcity.game.battle.pve.template.StageTemplate;
import com.xcity.game.common.PlayerPool;
import com.xcity.game.vip.VipTemplate;
import com.xcity.game.vip.VipUtils;

import naga.x.App;
import naga.x.game.template.TemplateService;
import naga.x.game.time.TimeUpdater;

/**
 * pve关卡信息
 * 
 * @author yang.li
 *
 */
public class PVEStage implements Comparable<PVEStage> {

	@JSONField(name = "0")
	protected int id;
	@JSONField(serialize = false)
	protected StageTemplate template;
	@JSONField(name = "1")
	protected int star;
	@JSONField(name = "2")
	protected long passTime;
	@JSONField(name = "3")
	protected int lastFightDay; // 最近一次挑战日期
	@JSONField(name = "4")
	protected int foughtTimesToday; // 今日已挑战次数
	@JSONField(name = "5")
	protected byte gotBox; // 是否领取关卡宝箱 0未领取 1已领取
	@JSONField(name = "6")
	protected int resetCount;// 今日重置次数
	@JSONField(name = "7")
	protected int resetCountDay;// 最近一次重置次数日子
	
	@JSONField(serialize = false)
	protected int subIndex;//大关内的索引 从0开始

	public PVEStage() {}
	
	public PVEStage(StageTemplate template) {
		this.id = template.getId();
		this.template = template;
	}
	
	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public StageTemplate getTemplate() {
		return template != null ? template : (template = App.getApp().getServiceManager().get(TemplateService.class).get(StageTemplate.class, id));
	}

	public void setTemplate(StageTemplate template) {
		this.template = template;
	}

	public int getStar() {
		return star;
	}

	public void setStar(int star) {
		this.star = star;
	}

	public long getPassTime() {
		return passTime;
	}

	public void setPassTime(long passTime) {
		this.passTime = passTime;
	}

	public int getLastFightDay() {
		return lastFightDay;
	}

	public void setLastFightDay(int lastFightDay) {
		this.lastFightDay = lastFightDay;
	}

	public int getFoughtTimesToday() {
		return foughtTimesToday;
	}

	public void setFoughtTimesToday(int foughtTimesToday) {
		this.foughtTimesToday = foughtTimesToday;
	}

	public int getResetCount() {
		return resetCount;
	}

	public void setResetCount(int resetCount) {
		this.resetCount = resetCount;
	}

	public int getResetCountDay() {
		return resetCountDay;
	}

	public void setResetCountDay(int resetCountDay) {
		this.resetCountDay = resetCountDay;
	}

	/**
	 * 0未领取 1已领取
	 */
	public byte getGotBox() {
		return gotBox;
	}
	
	public void setGotBox(byte gotBox) {
		this.gotBox = gotBox;
	}
	
	
	@JSONField(serialize = false)
	public int getLeftTimes(Player player) {
		int total = getTemplate().getNumOfPerDay();
		if (total > 0) {
			VipTemplate vt = VipUtils.getTemplate(player.getVipLvl());
			if (vt != null) total += vt.getStageAddedNum();
		}
		if (total < 0 || lastFightDay != TimeUpdater.getInstance().today()) {
			return total;
		}
		int c = total - foughtTimesToday;
		return c > 0 ? c : 0;
	}
	
	@JSONField(serialize = false)
	public boolean isNormal(){
		if(this.getTemplate().getType() == PVEManager.STAGE_TYPE_NORMAL || this.getTemplate().getType() == PVEManager.STAGE_TYPE_BOSS){
			return true;
		}
		return false;
	}
	
	@JSONField(serialize = false)
	public boolean isPass() {
		return star > 0 && passTime > 0;
	}
	
	public void end(int star) {
		if (this.star == 0 && star > 0) { // 第一次通过
			passTime = TimeUpdater.getInstance().now();
		}
		if (star > this.star) { // 记录最高星数
			this.star = star;
		}
//		this.star = this.star | star;
		if (star > 0) { // 挑战成功才记录次数(失败不记录)
			int today = TimeUpdater.getInstance().today();
			if (lastFightDay != today) {
				lastFightDay = today;
				foughtTimesToday = 0;
			}
			foughtTimesToday++;
		}
	}

	/**
	 * 精英关索引 从0开始 0~9
	 */
	@JSONField(serialize = false)
	public int getSubIndex() {
		return subIndex;
	}

	@JSONField(serialize = false)
	public void setSubIndex(int subIndex) {
		this.subIndex = subIndex;
	}

	@JSONField(serialize = false)
	public int getGotStar(int starDetail){
		int count = 0;
		for(int i = 0; i < StageTemplate.CONDITION_COUNT + 1; i++){
			if((starDetail & (1 << i)) > 0){
				count++;
			}
		}
		return count;
	}
	

	/**
	 * 0没有宝箱 1有宝箱不可领取 2可领未领取 3已领取
	 */
	@JSONField(serialize = false)
	public byte getStageBoxState(){
		byte ret = 0;
		if(this.getTemplate().getStageBoxId() != 0){
			if(this.isPass()){
				if(this.getGotBox() == 0){
					ret = 2;
				}else{
					ret = 3;
				}
			}else{
				ret = 1;
			}
		}
		return ret;
	}
	

	@JSONField(serialize = false)
	/**
	 * 剩余重置副本次数
	 * @param type 0普通本 1精英
	 * @return
	 */
	public int getTodayResetCount(Player player, byte type){
		int today = TimeUpdater.getInstance().today();
		int resetCount;
		int resetDay;
		if(type == 0){
			resetCount = getResetCount();//player.getPool().getInt(PlayerPool.PROPERTY_PVE_FULL_NORMAL_COUNT, 0);
			resetDay = getResetCountDay();//player.getPool().getInt(PlayerPool.PROPERTY_PVE_FULL_NORMAL_DAY, 0);
		}else{
			resetCount = getResetCount();//player.getPool().getInt(PlayerPool.PROPERTY_PVE_FULL_HERO_COUNT, 0);
			resetDay = getResetCountDay();//player.getPool().getInt(PlayerPool.PROPERTY_PVE_FULL_HERO_DAY, 0);
		}
		if(resetDay != today){
			resetCount = 0;
		}
		return resetCount;
	}
	
	@Override
	public int compareTo(PVEStage o) {
		return getId() - o.getId();
	}

}
